#author : wx
#excel to c# class
import sys
import xlrd
import os

Indir = "./"
Outdir = "./"

def parse_excel(excelFile):

    excel = xlrd.open_workbook(excelFile)

    code = "//此文件自动生成，不要修改\n"
    code += "\nnamespace AutoJsonClass\n{"
    code += "\n\tpublic class "
    fileName = excelFile.split('.')[0]
    code += fileName
    code += "\n\t{"

    sheet = excel.sheet_by_index(0)
    table1 = sheet.row_values(0) #获取第1行的数据
    table2 = sheet.row_values(1) #获取第2行的数据
    num = len(table1)
    for i in range(0,num,1):
        code += "\n\t\tpublic "
        if table1[i] =="" :
            table1[i] = "string"
        code += table1[i]
        code += " "
        code += table2[i]
        code += " { get; set; }"
        
    code += "\n\t}\n}"
    fileNameAll = fileName+".cs"
    r=open(os.path.join(Outdir,fileNameAll),"x",encoding='utf-8')
    r.write(code)
    r.close

    print("创建 {0} 完成!".format(fileNameAll))

if __name__ == '__main__':
    num = len(sys.argv)
    Indir = sys.argv[1]
    Outdir = sys.argv[2]
    excelFiles = []
    files = os.listdir(Indir)
    for file in files:
        if file[0]=='~':#忽略打开的Excel文件产生的临时文件
            continue
        if file.endswith('xlsx') or file.endswith('xls'):
            excelFiles.append(file)

    print ("正在删除目标目录{0}下所有文件!".format(os.path.abspath(Outdir)))

    deleFiles = os.listdir(Outdir)
    for file in deleFiles:
        os.remove(os.path.join(Outdir,file))
        print("删除{0}".format(file))
    
    print ("\n当前目录有{0}个excel文件,开始创建...".format(len(excelFiles)))
    for excelFile in excelFiles:
        parse_excel(excelFile)